package com.kolon.business.mngr.client.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.business.mngr.client.dao.ClientDao;
import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mngr.client.controller.ClientFileDownloadController")
public class ClientFileDownloadController implements SimpleController {
	
	@Resource(name="com.kolon.business.mngr.client.dao.ClientDaoImpl")
	private ClientDao clientDao;
	
	public ModelAndView controller(HttpServletRequest request,
		HttpServletResponse response) throws Exception {
		return null;
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/business/mngr/client/clientDownload.do")
	public ModelAndView servDownload(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		Map<String, String> map = new HashMap<String, String>();
		ModelAndView mv = new ModelAndView("downloadView");
		// 파라미터
		String frfTp = request.getParameter("frfTp");
		String fileNum = request.getParameter("fileNum");
		map.put("frfTp", frfTp);
		// 정보 조회
		Map servMap = clientDao.getMap(map);
		File downloadFile = new File(MsgUtil.getMessage("app.file.path") + ((String)servMap.get("FRF_TP")).toLowerCase()
				+ File.separator + Constant.APP_CNT_NM + File.separator + servMap.get("FILE_S_NM_"+fileNum));
		mv.addObject("downloadFile", downloadFile);
		mv.addObject("fileName", servMap.get("FILE_R_NM_"+fileNum));
		// 리턴
		return mv;
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping(value="/business/mngr/client/clientHstyDownload.do")
	public ModelAndView appDownload(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		Map<String, String> map = new HashMap<String, String>();
		ModelAndView mv = new ModelAndView("downloadView");
		// 파라미터
		String appHstyCntNo = request.getParameter("appHstyCntNo");
		String fileNum = request.getParameter("fileNum");
		map.put("appHstyCntNo", appHstyCntNo);
		// 정보 조회
		Map cntHstyMap = clientDao.getHstyCntMap(map);
		File downloadFile = new File(MsgUtil.getMessage("app.file.path") + ((String)cntHstyMap.get("FRF_TP")).toLowerCase() + 
				File.separator + Constant.APP_CNT_NM + File.separator + cntHstyMap.get("FRF_VER") + File.separator + cntHstyMap.get("FILE_S_NM_"+fileNum));
		mv.addObject("downloadFile", downloadFile);
		mv.addObject("fileName", cntHstyMap.get("FILE_R_NM_"+fileNum));
		// 리턴
		return mv;
	}
	
}

